#include "common/common.h"

struct UserInfo{
    char name[50];
    char phone[20];
};


int main(){
    char ip[20] = "127.0.0.1";
    int port = 10001;

    struct sockaddr_in server, client;
    memset(&server, 0, sizeof(server));
    server.sin_family = AF_INET;
    server.sin_port = htons(port);
    server.sin_addr.s_addr = htonl(INADDR_ANY);
    
    int listenfd;
    listenfd = socket(AF_INET, SOCK_STREAM, 0);
    bind(listenfd, (struct sockaddr*)&server, sizeof(server));

    printf("i will listen\n");
    listen(listenfd, 10);

    printf("I will accept\n");
    socklen_t len = sizeof(client);
    int accpfd = accept(listenfd, (struct sockaddr*)&client, &len);
    printf("accept is %d\n", accpfd);

    char name[50] = {0};
    char phone[20] = {0};
    struct UserInfo info;

    int i = 1;
    send(accpfd, &i, sizeof(int), 0);
    printf("%ld\n", recv(accpfd, phone, sizeof(phone), 0));
    printf("%ld\n", strlen(phone));
    printf("recv : %s\n", phone);

    i = 2;
    send(accpfd, &i, sizeof(int), 0);
    recv(accpfd, name, sizeof(name), 0);
    printf("recv : %s\n", name);

    i = 3;
    send(accpfd, &i, sizeof(int), 0);
    recv(accpfd, &info, sizeof(info), 0);
    printf("recv : %s, %s\n", info.name, info.phone);

    i = 4;
    send(accpfd, &i, sizeof(int), 0);
    while(1){
        sleep(2);
    }
    return 0;
}